package com.alks.function.service.impl.stockdept.pcfactoryquery;

import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.PcSlipDetail;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.stockdept.pcfactoryquery.SurplusIoDto;
import com.alks.function.data.request.stockdept.pcfactoryquery.SurplusIoRequest;
import com.alks.function.mapper.pcfactoryquery.FT_QR_PcSlipDetailMapper;
import com.alks.function.service.stockdept.pcfactoryquery.IOtherMtlOService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2024/8/12下午5:33
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OtherMtlOServiceImpl extends ServiceImpl<FT_QR_PcSlipDetailMapper, PcSlipDetail> implements IOtherMtlOService {
    private final FT_QR_PcSlipDetailMapper detailMapper;

    @Override
    public ResponseInfo temp(SurplusIoRequest request, String slipType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        List<PcSlipDetail> details = lambdaQuery()
                .like(StringUtil.isNotBlank(request.getSlipNo()), PcSlipDetail::getSlipNo, request.getSlipNo())
                .like(StringUtil.isNotBlank(request.getLotNo()), PcSlipDetail::getLotNo, request.getLotNo())
                .eq(PcSlipDetail::getSlipType, slipType)
                .eq(StringUtil.isNotBlank(request.getDeptNo()), PcSlipDetail::getDeptNo, request.getDeptNo())
                .like(StringUtil.isNotBlank(request.getMtlName()), PcSlipDetail::getMtlName, request.getMtlName())
                .eq(StringUtil.isNotBlank(request.getSlipReason()), PcSlipDetail::getSlipReason, request.getSlipReason())
                .like(StringUtil.isNotBlank(request.getSysUser()), PcSlipDetail::getSysUser, request.getSysUser())
                .like(StringUtil.isNotBlank(request.getMtlType()), PcSlipDetail::getMtlNo, request.getMtlType())
                .ge(request.getSlipDateStart() != null, PcSlipDetail::getSlipDate, request.getSlipDateStart())
                .le(request.getSlipDateStart() != null, PcSlipDetail::getSlipDate, request.getSlipDateStart())
                .eq(PcSlipDetail::getCompanyId,companyId).list();
        Page<PcSlipDetail> page=(Page<PcSlipDetail>) details;
        /*封装返回*/
        List<SurplusIoDto> dtos = BeanUtil.copyList(details, SurplusIoDto.class);
        Map<String,Object> map=new HashMap<>(2);
        map.put("dto",dtos);
        map.put("total", page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo back(SurplusIoRequest request, String slipType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取出入库原因*/
        List<String> slipReason = lambdaQuery()
                .select(PcSlipDetail::getSlipReason)
                .like(StringUtil.isNotBlank(request.getSlipNo()), PcSlipDetail::getSlipNo, request.getSlipNo())
                .like(StringUtil.isNotBlank(request.getLotNo()), PcSlipDetail::getLotNo, request.getLotNo())
                .eq(PcSlipDetail::getSlipType, slipType)
                .eq(StringUtil.isNotBlank(request.getDeptNo()), PcSlipDetail::getDeptNo, request.getDeptNo())
                .like(StringUtil.isNotBlank(request.getMtlName()), PcSlipDetail::getMtlName, request.getMtlName())
                .eq(StringUtil.isNotBlank(request.getSlipReason()), PcSlipDetail::getSlipReason, request.getSlipReason())
                .like(StringUtil.isNotBlank(request.getSysUser()), PcSlipDetail::getSysUser, request.getSysUser())
                .like(StringUtil.isNotBlank(request.getMtlType()), PcSlipDetail::getMtlNo, request.getMtlType())
                .ge(request.getSlipDateStart() != null, PcSlipDetail::getSlipDate, request.getSlipDateStart())
                .le(request.getSlipDateStart() != null, PcSlipDetail::getSlipDate, request.getSlipDateStart())
                .eq(PcSlipDetail::getCompanyId, companyId)
                .groupBy(PcSlipDetail::getSlipReason)
                .orderByDesc(PcSlipDetail::getSlipReason).list()
                .stream().filter(Objects::nonNull)
                .map(PcSlipDetail::getSlipReason)
                .collect(Collectors.toList());
        /*获取部门*/
        List<Object> dept = lambdaQuery()
                .select(PcSlipDetail::getDeptName, PcSlipDetail::getDeptNo)
                .like(StringUtil.isNotBlank(request.getSlipNo()), PcSlipDetail::getSlipNo, request.getSlipNo())
                .like(StringUtil.isNotBlank(request.getLotNo()), PcSlipDetail::getLotNo, request.getLotNo())
                .eq(PcSlipDetail::getSlipType, slipType)
                .eq(StringUtil.isNotBlank(request.getDeptNo()), PcSlipDetail::getDeptNo, request.getDeptNo())
                .like(StringUtil.isNotBlank(request.getMtlName()), PcSlipDetail::getMtlName, request.getMtlName())
                .eq(StringUtil.isNotBlank(request.getSlipReason()), PcSlipDetail::getSlipReason, request.getSlipReason())
                .like(StringUtil.isNotBlank(request.getSysUser()), PcSlipDetail::getSysUser, request.getSysUser())
                .like(StringUtil.isNotBlank(request.getMtlType()), PcSlipDetail::getMtlNo, request.getMtlType())
                .ge(request.getSlipDateStart() != null, PcSlipDetail::getSlipDate, request.getSlipDateStart())
                .le(request.getSlipDateStart() != null, PcSlipDetail::getSlipDate, request.getSlipDateStart())
                .eq(PcSlipDetail::getCompanyId, companyId)
                .groupBy(PcSlipDetail::getDeptNo)
                .orderByDesc(PcSlipDetail::getDeptNo).list()
                .stream().filter(Objects::nonNull)
                .map(a -> {
                    Map<String, String> map = new HashMap<>(2);
                    map.put("deptName", a.getDeptName());
                    map.put("deptNo", a.getDeptNo());
                    return map;
                })
                .collect(Collectors.toList());
        /*封装返回*/
        Map<String,Object> map = new HashMap<>(2);
        map.put("slipReason",slipReason);
        map.put("dept",dept);
        return ResponseInfo.ok(map);
    }
}
